

// httpRequest
var httpRequest;

// store ajax status
var waitForResult = false;
var waitForResultTimer;
function isWait(){
    return waitForResult;
}

function setWait(){
    waitForResult = true;
    clearTimeout(waitForResultTimer);
    controlWait();
}

function cleanWait(control){
    if(control && waitForResult){
        setDebugMessage("AJAX doesn't have an answer!!!")
    }
    waitForResult = false;
}

function controlWait(){
    waitForResultTimer = setTimeout("cleanWait(true);", 5000);
}


if (!document.ELEMENT_NODE) {
    document.ELEMENT_NODE = 1;
    document.ATTRIBUTE_NODE = 2;
    document.TEXT_NODE = 3;
    document.CDATA_SECTION_NODE = 4;
    document.ENTITY_REFERENCE_NODE = 5;
    document.ENTITY_NODE = 6;
    document.PROCESSING_INSTRUCTION_NODE = 7;
    document.COMMENT_NODE = 8;
    document.DOCUMENT_NODE = 9;
    document.DOCUMENT_TYPE_NODE = 10;
    document.DOCUMENT_FRAGMENT_NODE = 11;
    document.NOTATION_NODE = 12;
}

/**
 * by http://www.alistapart.com/articles/crossbrowserscripting/
 *  Anthony T. Holdener III
 */
document._importNode = function(node, allChildren){
    switch (node.nodeType) {
        case document.ELEMENT_NODE:
            var i;
            var newNode = document.createElement(node.nodeName);
            var lenOfAttr = node.attributes.length;
            /* does the node have any attributes to add? */
            if (node.attributes && lenOfAttr > 0) {
                for (i = 0; i < lenOfAttr; i++) {
                    var nodeName = node.attributes[i].nodeName;
                    //var nodeName2 = node.attributes[i].nodeName;
                    var nodeValue = node.getAttribute(nodeName);
                    // IE hack
                    if (nodeName == "class") {
                        newNode.className = nodeValue;
                    }
                    else {
                        newNode.setAttribute(nodeName, nodeValue);
                    }
                }
            }
            /* are we going after children too, and does the node have any? */
            if (allChildren && node.childNodes && node.childNodes.length > 0) {
                lenOfAttr = node.childNodes.length;
                for (i = 0; i < lenOfAttr; i++) {
                    newNode.appendChild(document._importNode(node.childNodes[i], allChildren));
                }
            }
            return newNode;
        case document.TEXT_NODE:
        case document.CDATA_SECTION_NODE:
        case document.COMMENT_NODE:
            return document.createTextNode(node.nodeValue);
            
    }
};


/**
 * utils for returning the first child node
 */
function getFirstNode(node){
    var child;
    for (var i = 0; (child = node.childNodes[i]); i++) {
        if (child.nodeType == 1) { return child; }
    }
    return node;
}

/**
 * extract a text value from node
 */
function getText(node){
    var result = "";
    if (node != null) {
        if (node.nodeType == 3) {
            result = node.data;
        }
        else if (node.nodeType == 1) {
            if (node.nodeName == 'TEXTAREA') {
                result = node.value;
            }
            else if (node.nodeName == 'INPUT') {
                result = node.value;
            }
            else {
                result = getText(node.firstChild);
            }
        }
    }else{
        setDebugMessage("getText(null)");
    }
    return result;
}

function setText(node, value){
    if (node != null) {
        if (node.nodeType == 3) {
            node.data = value;
        }
        else if (node.nodeType == 1) {
            if (node.firstChild == null) {
                node.appendChild(document.createTextNode(value));
            }
            else {
                node.firstChild.data = value;
            }
        }
    }
}

/**
 * create request for ajax
 */
function getHTTPRequestObject(){
    var xmlHttpRequest;
    if (!xmlHttpRequest && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlHttpRequest = new XMLHttpRequest();
        } 
        catch (exception) {
            setDebugMessage('getHTTPRequestObject error: ' + exception);
        }
    }
    else {
        setDebugMessage('getHTTPRequestObject error');
    }
    return xmlHttpRequest;
}

function isNull(obj, _this){
    if (obj == null) {
        setDebugMessage("isNull " + _this);
    }
    return obj == null;
}


/**
 * send request
 */
function sendRequest(url, parameters, callBackFunction){
    httpRequest.onreadystatechange = callBackFunction;
    //  httpRequest.overrideMimeType('text/html');  TODO: problem with IE
    httpRequest.open('POST', url, true);
    httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    httpRequest.setRequestHeader("Content-length", parameters.length);
    httpRequest.setRequestHeader("Connection", "close");
    httpRequest.send(parameters);
}

function addEvent(obj, evType, fn){
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    }
    else if (obj.attachEvent) {
        var r = obj.attachEvent('on' + evType, fn);
        return r;
    }
    else { return false; }
}
